home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / libtmc / prstr.c < prev    next >
C/C++ Source or Header  |  1990-11-06  |  2KB  |  80 lines

  1. /* 
  2.    Copyright (C) 1990 C van Reewijk, email: dutentb.uucp!reeuwijk
  3.  
  4. This file is part of GLASS.
  5.  
  6. GLASS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GLASS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GLASS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* File: prstr.c
  21.  * C. van Reeuwijk
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26.  
  27. #include "tmc.h"
  28.  
  29. #define BACKSLASH '\\'
  30.  
  31. #define STRBUFSZ 1000
  32.  
  33. /* definition of 'print' for a 'string' */
  34. void print_string( s )
  35.  register char *s;
  36. {
  37.     char buf[STRBUFSZ+10];
  38.     register char *bufp;
  39.     register char *bufend;
  40.     register int c;
  41.  
  42.     if( s == (char *)0 ){
  43.     printword( "<nil>" );
  44.     return;
  45.     }
  46.     bufp = buf;
  47.     bufend = &buf[STRBUFSZ];
  48.     *bufp++ = '"';
  49.     while( *s != '\0' ){
  50.     c = *s++;
  51.     while( c<0 ) c += 256;
  52.     if( c == BACKSLASH || c == '"' ){
  53.         *bufp++ = BACKSLASH;
  54.         *bufp++ = c;
  55.         continue;
  56.     }
  57.     if( c>=' ' && c<='~' ){
  58.         *bufp++ = c;
  59.         continue;
  60.     }
  61.     switch( c ){
  62.         case '\b': *bufp++ = BACKSLASH; *bufp++ = 'b'; break;
  63.         case '\f': *bufp++ = BACKSLASH; *bufp++ = 'f'; break;
  64.         case '\n': *bufp++ = BACKSLASH; *bufp++ = 'n'; break;
  65.         case '\r': *bufp++ = BACKSLASH; *bufp++ = 'r'; break;
  66.         case '\t': *bufp++ = BACKSLASH; *bufp++ = 't'; break;
  67.         case '\v': *bufp++ = BACKSLASH; *bufp++ = 'v'; break;
  68.         default:
  69.         (void) sprintf( bufp, "\\%03o", (c & 0xff) );
  70.         while( *bufp != '\0' ) bufp++;
  71.     }
  72.     if( bufp>=bufend ){
  73.         tmfatal( __FILE__, __LINE__, "string too long" );
  74.     }
  75.     }
  76.     *bufp++ = '"';
  77.     *bufp = '\0';
  78.     printword( buf );
  79. }
  80.